/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.uniqueness;

import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.net.Uri;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.R;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.ImmutableCell;
import com.onegravity.sudoku.solver.util.BitSetFactory;
import com.onegravity.sudoku.solver.util.HtmlLoader;
import com.onegravity.sudoku.solver.util.ValuesFormatter;

public class UniquenessTestType2and5Hint extends UniquenessTestHint {

	private static final long serialVersionUID = -3319373472323593543L;
	
	private final Cell[] cells;
    private final int value;
    private final boolean isType5;

    public UniquenessTestType2and5Hint(List<Cell> loop, int v1, int v2,
            Map<Cell, BitSet> removablePotentials, Cell[] cells, int value, boolean isType5) {
        super(loop, v1, v2, removablePotentials);
        this.cells = cells;
        this.value = value;
        this.isType5 = isType5;
    }

    @Override
    public Collection<Cell> getSelectedCells(HintType hintType) {
    	switch(hintType) {
    	case SMALL_CLUE: return super.getSelectedCells(hintType);
    	default:
    	case BIG_CLUE: return null;
    	case HINT: 
    		if (cells==null) return Collections.emptyList();
    		return Arrays.asList(cells);
    	}
    }

    @Override
    public Map<Cell, BitSet> getSelectedPotentials(HintType hintType, int viewNum) {
        return (hintType!=HintType.SMALL_CLUE) ? super.getSelectedPotentials(hintType, viewNum) : null;
    }

    @Override
    public Map<Cell, BitSet> getSecondarySelectedPotentials(HintType hintType, int viewNum) {
        Map<Cell, BitSet> result = new HashMap<Cell, BitSet>();
        for (Cell cell : cells) {
        	result.put(cell, BitSetFactory.createOneElement(value));
        }
        return (hintType==HintType.HINT) ? result : null;
    }

    @Override
    public Map<ImmutableCell, BitSet> getRemovedPotentials(HintType hintType) {
        return (hintType==HintType.HINT) ? super.getRemovedPotentials(hintType) : null;
    }
    
    @Override
    protected int getType() {
        return isType5 ? 5 : 2;
    }
    
    @Override
    protected HintResource getResource() {
        return isType5 ? HintResource.UNIQUENESS_TEST_5 : HintResource.UNIQUENESS_TEST_2;
    }

    @Override
    public Uri getLink() {
    	return getResource().getUrl();
	}

   @Override
    public String getHintHtml(HintType hintType) {
        String result = HtmlLoader.loadHtml(getResource().hint(hintType));
        String type = getTypeName();
        Cell[] loopCells = new Cell[loop.size()];
        loop.toArray(loopCells);
        String allCells = Cell.toString(loopCells);
        String andString = HintResource.getString(R.string.and);
        String orString = HintResource.getString(R.string.or);
        String extraCellsOr = ValuesFormatter.formatCells(cells, " " + orString + " "); //$NON-NLS-1$ //$NON-NLS-2$
        String extraCellsAnd = ValuesFormatter.formatCells(cells, " " + andString + " "); //$NON-NLS-1$ //$NON-NLS-2$
        result = HtmlLoader.format(result, type, v1, v2, allCells, extraCellsOr, extraCellsAnd, value, isType5 ? "5" : "2", loop.size(), cells.length); //$NON-NLS-1$ //$NON-NLS-2$
        return result;
    }
 
}